<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Initializer Lists</title>
<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../index.html" title="Chapter 1. Boost.JSON">
<link rel="up" href="../dom.html" title="Document Model">
<link rel="prev" href="conversion.html" title="Value Conversion">
<link rel="next" href="../allocators.html" title="Allocators">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="conversion.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../dom.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../allocators.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="json.dom.initializer_lists"></a><a class="link" href="initializer_lists.html" title="Initializer Lists">Initializer Lists</a>
</h3></div></div></div>
<p>
        Initializer lists can be used to construct or assign a <a class="link" href="../ref/boost__json__value.html" title="value"><code class="computeroutput"><span class="identifier">value</span></code></a>:
      </p>
<pre class="programlisting"><span class="identifier">value</span> <span class="identifier">jv</span> <span class="special">=</span> <span class="special">{</span>
    <span class="special">{</span> <span class="string">"name"</span><span class="special">,</span> <span class="string">"John Doe"</span> <span class="special">},</span>
    <span class="special">{</span> <span class="string">"active"</span><span class="special">,</span> <span class="keyword">true</span> <span class="special">},</span>
    <span class="special">{</span> <span class="string">"associated-accounts"</span><span class="special">,</span> <span class="keyword">nullptr</span> <span class="special">},</span>
    <span class="special">{</span> <span class="string">"total-balance"</span><span class="special">,</span> <span class="number">330.00</span> <span class="special">},</span>
    <span class="special">{</span> <span class="string">"account-balances"</span><span class="special">,</span> <span class="special">{</span> <span class="number">84</span><span class="special">,</span> <span class="number">120</span><span class="special">,</span> <span class="number">126</span> <span class="special">}</span> <span class="special">}</span> <span class="special">};</span>
</pre>
<p>
        Simple initializer lists produce an <a class="link" href="../ref/boost__json__array.html" title="array"><code class="computeroutput"><span class="identifier">array</span></code></a>:
      </p>
<pre class="programlisting"><span class="identifier">value</span> <span class="identifier">jv</span> <span class="special">=</span> <span class="special">{</span> <span class="keyword">true</span><span class="special">,</span> <span class="number">2</span><span class="special">,</span> <span class="string">"hello"</span><span class="special">,</span> <span class="keyword">nullptr</span> <span class="special">};</span>

<span class="identifier">assert</span><span class="special">(</span> <span class="identifier">jv</span><span class="special">.</span><span class="identifier">is_array</span><span class="special">()</span> <span class="special">);</span>

<span class="identifier">assert</span><span class="special">(</span> <span class="identifier">jv</span><span class="special">.</span><span class="identifier">as_array</span><span class="special">().</span><span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="number">4</span> <span class="special">);</span>

<span class="identifier">assert</span><span class="special">(</span> <span class="identifier">serialize</span><span class="special">(</span><span class="identifier">jv</span><span class="special">)</span> <span class="special">==</span> <span class="string">"[true,2,\"hello\",null]"</span> <span class="special">);</span>
</pre>
<p>
        Initializer lists can be nested. Here we construct an array as an element
        of an array:
      </p>
<pre class="programlisting"><span class="identifier">value</span> <span class="identifier">jv</span> <span class="special">=</span> <span class="special">{</span> <span class="keyword">true</span><span class="special">,</span> <span class="number">2</span><span class="special">,</span> <span class="string">"hello"</span><span class="special">,</span> <span class="special">{</span> <span class="string">"bye"</span><span class="special">,</span> <span class="keyword">nullptr</span><span class="special">,</span> <span class="keyword">false</span> <span class="special">}</span> <span class="special">};</span>

<span class="identifier">assert</span><span class="special">(</span> <span class="identifier">jv</span><span class="special">.</span><span class="identifier">is_array</span><span class="special">()</span> <span class="special">);</span>

<span class="identifier">assert</span><span class="special">(</span> <span class="identifier">jv</span><span class="special">.</span><span class="identifier">as_array</span><span class="special">().</span><span class="identifier">back</span><span class="special">().</span><span class="identifier">is_array</span><span class="special">()</span> <span class="special">);</span>

<span class="identifier">assert</span><span class="special">(</span> <span class="identifier">serialize</span><span class="special">(</span><span class="identifier">jv</span><span class="special">)</span> <span class="special">==</span> <span class="string">"[true,2,\"hello\",[\"bye\",null,false]]"</span> <span class="special">);</span>
</pre>
<p>
        When a two element initializer list is nested within an enclosing initializer
        list, it is unclear whether it represents an <a class="link" href="../ref/boost__json__array.html" title="array"><code class="computeroutput"><span class="identifier">array</span></code></a> or an <a class="link" href="../ref/boost__json__object.html" title="object"><code class="computeroutput"><span class="identifier">object</span></code></a>:
      </p>
<pre class="programlisting"><span class="comment">// Should this be an array or an object?</span>
<span class="identifier">value</span> <span class="identifier">jv</span> <span class="special">=</span> <span class="special">{</span> <span class="special">{</span> <span class="string">"hello"</span><span class="special">,</span> <span class="number">42</span> <span class="special">},</span> <span class="special">{</span> <span class="string">"world"</span><span class="special">,</span> <span class="number">43</span> <span class="special">}</span> <span class="special">};</span>
</pre>
<p>
        In such cases, if every element consists of a string followed by a single
        value, then the enclosing initializer list is interpreted as an <a class="link" href="../ref/boost__json__object.html" title="object"><code class="computeroutput"><span class="identifier">object</span></code></a>. Otherwise, it is interpreted
        as an <a class="link" href="../ref/boost__json__array.html" title="array"><code class="computeroutput"><span class="identifier">array</span></code></a>.
      </p>
<pre class="programlisting"><span class="identifier">value</span> <span class="identifier">jv1</span> <span class="special">=</span> <span class="special">{</span> <span class="special">{</span> <span class="string">"hello"</span><span class="special">,</span> <span class="number">42</span> <span class="special">},</span> <span class="special">{</span> <span class="string">"world"</span><span class="special">,</span> <span class="number">43</span> <span class="special">}</span> <span class="special">};</span>

<span class="identifier">assert</span><span class="special">(</span> <span class="identifier">jv1</span><span class="special">.</span><span class="identifier">is_object</span><span class="special">()</span> <span class="special">);</span>

<span class="identifier">assert</span><span class="special">(</span> <span class="identifier">jv1</span><span class="special">.</span><span class="identifier">as_object</span><span class="special">().</span><span class="identifier">size</span><span class="special">()</span> <span class="special">==</span> <span class="number">2</span> <span class="special">);</span>

<span class="identifier">assert</span><span class="special">(</span> <span class="identifier">serialize</span><span class="special">(</span><span class="identifier">jv1</span><span class="special">)</span> <span class="special">==</span> <span class="identifier">R</span><span class="string">"({"</span><span class="identifier">hello</span><span class="string">":42,"</span><span class="identifier">world</span><span class="string">":43})"</span> <span class="special">);</span>

<span class="comment">// All of the following are arrays</span>

<span class="identifier">value</span> <span class="identifier">jv2</span> <span class="special">=</span> <span class="special">{</span> <span class="special">{</span> <span class="string">"make"</span><span class="special">,</span> <span class="string">"Tesla"</span> <span class="special">},</span> <span class="special">{</span> <span class="string">"model"</span><span class="special">,</span> <span class="number">3</span> <span class="special">},</span> <span class="string">"black"</span> <span class="special">};</span>

<span class="identifier">value</span> <span class="identifier">jv3</span> <span class="special">=</span> <span class="special">{</span> <span class="special">{</span> <span class="string">"library"</span><span class="special">,</span> <span class="string">"JSON"</span> <span class="special">},</span> <span class="special">{</span> <span class="string">"Boost"</span><span class="special">,</span> <span class="string">"C++"</span><span class="special">,</span> <span class="string">"Fast"</span><span class="special">,</span> <span class="string">"JSON"</span> <span class="special">}</span> <span class="special">};</span>

<span class="identifier">value</span> <span class="identifier">jv4</span> <span class="special">=</span> <span class="special">{</span> <span class="special">{</span> <span class="string">"color"</span><span class="special">,</span> <span class="string">"blue"</span> <span class="special">},</span> <span class="special">{</span> <span class="number">1</span><span class="special">,</span> <span class="string">"red"</span> <span class="special">}</span> <span class="special">};</span>

<span class="identifier">assert</span><span class="special">(</span> <span class="identifier">jv2</span><span class="special">.</span><span class="identifier">is_array</span><span class="special">()</span> <span class="special">&amp;&amp;</span> <span class="identifier">jv3</span><span class="special">.</span><span class="identifier">is_array</span><span class="special">()</span> <span class="special">&amp;&amp;</span> <span class="identifier">jv4</span><span class="special">.</span><span class="identifier">is_array</span><span class="special">()</span> <span class="special">);</span>
</pre>
<p>
        To resolve the ambiguity manually, use an explicit constructor:
      </p>
<pre class="programlisting"><span class="identifier">value</span> <span class="identifier">jv</span> <span class="special">=</span> <span class="special">{</span> <span class="special">{</span> <span class="string">"hello"</span><span class="special">,</span> <span class="number">42</span> <span class="special">},</span> <span class="identifier">array</span><span class="special">{</span> <span class="string">"world"</span><span class="special">,</span> <span class="number">43</span> <span class="special">}</span> <span class="special">};</span>

<span class="identifier">assert</span><span class="special">(</span> <span class="identifier">jv</span><span class="special">.</span><span class="identifier">is_array</span><span class="special">()</span> <span class="special">);</span>

<span class="identifier">array</span><span class="special">&amp;</span> <span class="identifier">ja</span> <span class="special">=</span> <span class="identifier">jv</span><span class="special">.</span><span class="identifier">as_array</span><span class="special">();</span>

<span class="identifier">assert</span><span class="special">(</span> <span class="identifier">ja</span><span class="special">[</span><span class="number">0</span><span class="special">].</span><span class="identifier">is_array</span><span class="special">()</span> <span class="special">&amp;&amp;</span> <span class="identifier">ja</span><span class="special">[</span><span class="number">1</span><span class="special">].</span><span class="identifier">is_array</span><span class="special">());</span>

<span class="identifier">assert</span> <span class="special">(</span> <span class="identifier">serialize</span><span class="special">(</span><span class="identifier">jv</span><span class="special">)</span> <span class="special">==</span> <span class="identifier">R</span><span class="string">"([["</span><span class="identifier">hello</span><span class="string">",42],["</span><span class="identifier">world</span><span class="string">",43]])"</span> <span class="special">);</span>
</pre>
<p>
        Initializer lists can be used to unambiguously construct or assign an <a class="link" href="../ref/boost__json__object.html" title="object"><code class="computeroutput"><span class="identifier">object</span></code></a>
        or <a class="link" href="../ref/boost__json__array.html" title="array"><code class="computeroutput"><span class="identifier">array</span></code></a>:
      </p>
<pre class="programlisting"><span class="identifier">value</span> <span class="identifier">jv</span> <span class="special">=</span> <span class="special">{</span> <span class="special">{</span> <span class="string">"mercury"</span><span class="special">,</span> <span class="number">36</span> <span class="special">},</span> <span class="special">{</span> <span class="string">"venus"</span><span class="special">,</span> <span class="number">67</span> <span class="special">},</span> <span class="special">{</span> <span class="string">"earth"</span><span class="special">,</span> <span class="number">93</span> <span class="special">}</span> <span class="special">};</span>

<span class="identifier">assert</span><span class="special">(</span> <span class="identifier">jv</span><span class="special">.</span><span class="identifier">is_object</span><span class="special">()</span> <span class="special">);</span>

<span class="identifier">assert</span><span class="special">(</span> <span class="identifier">serialize</span><span class="special">(</span><span class="identifier">jv</span><span class="special">)</span> <span class="special">==</span> <span class="string">"{\"mercury\":36,\"venus\":67,\"earth\":93}"</span> <span class="special">);</span>

<span class="identifier">array</span> <span class="identifier">ja</span> <span class="special">=</span> <span class="special">{</span> <span class="special">{</span> <span class="string">"mercury"</span><span class="special">,</span> <span class="number">36</span> <span class="special">},</span> <span class="special">{</span> <span class="string">"venus"</span><span class="special">,</span> <span class="number">67</span> <span class="special">},</span> <span class="special">{</span> <span class="string">"earth"</span><span class="special">,</span> <span class="number">93</span> <span class="special">}</span> <span class="special">};</span>

<span class="identifier">assert</span><span class="special">(</span> <span class="identifier">serialize</span><span class="special">(</span><span class="identifier">ja</span><span class="special">)</span> <span class="special">==</span> <span class="string">"[[\"mercury\",36],[\"venus\",67],[\"earth\",93]]"</span> <span class="special">);</span>
</pre>
<p>
        Similarly, an initializer list for an <a class="link" href="../ref/boost__json__object.html" title="object"><code class="computeroutput"><span class="identifier">object</span></code></a> is always interpreted as
        an <a class="link" href="../ref/boost__json__object.html" title="object"><code class="computeroutput"><span class="identifier">object</span></code></a>.
        In such cases, the initializer list must be a list of key-value pairs. For
        example, the following code will not compile because <code class="computeroutput"><span class="number">1</span></code>
        is not convertible to a string:
      </p>
<pre class="programlisting"><span class="identifier">object</span> <span class="identifier">jo</span> <span class="special">=</span> <span class="special">{</span> <span class="special">{</span> <span class="number">1</span><span class="special">,</span> <span class="number">0.39</span> <span class="special">},</span> <span class="special">{</span> <span class="string">"venus"</span><span class="special">,</span> <span class="number">0.72</span> <span class="special">},</span> <span class="special">{</span> <span class="string">"earth"</span><span class="special">,</span> <span class="number">1</span> <span class="special">}</span> <span class="special">};</span>
</pre>
<p>
        The requirement for an initializer list to be interpreted as an <a class="link" href="../ref/boost__json__object.html" title="object"><code class="computeroutput"><span class="identifier">object</span></code></a> or <a class="link" href="../ref/boost__json__array.html" title="array"><code class="computeroutput"><span class="identifier">array</span></code></a> when initializing such an
        entity only applies to the outermost initializer list; subsequent nested
        elements will follow the usual ambiguity resolution rules.
      </p>
<pre class="programlisting"><span class="identifier">object</span> <span class="identifier">jo</span> <span class="special">=</span> <span class="special">{</span> <span class="special">{</span> <span class="string">"mercury"</span><span class="special">,</span> <span class="special">{</span> <span class="special">{</span> <span class="string">"distance"</span><span class="special">,</span> <span class="number">36</span> <span class="special">}</span> <span class="special">}</span> <span class="special">},</span> <span class="special">{</span> <span class="string">"venus"</span><span class="special">,</span> <span class="special">{</span> <span class="number">67</span><span class="special">,</span> <span class="string">"million miles"</span> <span class="special">}</span> <span class="special">},</span> <span class="special">{</span> <span class="string">"earth"</span><span class="special">,</span> <span class="number">93</span> <span class="special">}</span> <span class="special">};</span>

<span class="identifier">assert</span><span class="special">(</span> <span class="identifier">jo</span><span class="special">[</span><span class="string">"mercury"</span><span class="special">].</span><span class="identifier">is_object</span><span class="special">()</span> <span class="special">);</span>

<span class="identifier">assert</span><span class="special">(</span> <span class="identifier">jo</span><span class="special">[</span><span class="string">"venus"</span><span class="special">].</span><span class="identifier">is_array</span><span class="special">()</span> <span class="special">);</span>
</pre>
<p>
        Elements that are rvalues will be moved upon initialization:
      </p>
<pre class="programlisting"><span class="identifier">object</span> <span class="identifier">jo1</span> <span class="special">=</span> <span class="special">{</span> <span class="special">{</span> <span class="string">"john"</span><span class="special">,</span> <span class="number">100</span> <span class="special">},</span> <span class="special">{</span> <span class="string">"dave"</span><span class="special">,</span> <span class="number">500</span> <span class="special">},</span> <span class="special">{</span> <span class="string">"joe"</span><span class="special">,</span> <span class="number">300</span> <span class="special">}</span> <span class="special">};</span>

<span class="identifier">value</span> <span class="identifier">jv</span> <span class="special">=</span> <span class="special">{</span> <span class="special">{</span> <span class="string">"clients"</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">jo1</span><span class="special">)</span> <span class="special">}</span> <span class="special">};</span>

<span class="identifier">object</span><span class="special">&amp;</span> <span class="identifier">jo2</span> <span class="special">=</span> <span class="identifier">jv</span><span class="special">.</span><span class="identifier">as_object</span><span class="special">()[</span><span class="string">"clients"</span><span class="special">].</span><span class="identifier">as_object</span><span class="special">();</span>

<span class="identifier">assert</span><span class="special">(</span> <span class="special">!</span> <span class="identifier">jo2</span><span class="special">.</span><span class="identifier">empty</span><span class="special">()</span> <span class="special">&amp;&amp;</span> <span class="identifier">jo1</span><span class="special">.</span><span class="identifier">empty</span><span class="special">()</span> <span class="special">);</span>

<span class="identifier">assert</span><span class="special">(</span> <span class="identifier">serialize</span><span class="special">(</span><span class="identifier">jv</span><span class="special">)</span> <span class="special">==</span> <span class="identifier">R</span><span class="string">"({"</span><span class="identifier">clients</span><span class="string">":{"</span><span class="identifier">john</span><span class="string">":100,"</span><span class="identifier">dave</span><span class="string">":500,"</span><span class="identifier">joe</span><span class="string">":300}})"</span> <span class="special">);</span>
</pre>
<div class="warning"><table border="0" summary="Warning">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Warning]" src="../../../../../../doc/src/images/warning.png"></td>
<th align="left">Warning</th>
</tr>
<tr><td align="left" valign="top"><p>
          Do not create variables of type <a href="https://en.cppreference.com/w/cpp/utility/initializer_list" target="_top"><code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">initializer_list</span></code></a>. This may result
          in temporaries being destroyed before the initializer list is used.
        </p></td></tr>
</table></div>
<p>
        In all cases, the <a class="link" href="../ref/boost__json__storage_ptr.html" title="storage_ptr"><code class="computeroutput"><span class="identifier">storage_ptr</span></code></a> owned by an <a class="link" href="../ref/boost__json__object.html" title="object"><code class="computeroutput"><span class="identifier">object</span></code></a>, <a class="link" href="../ref/boost__json__array.html" title="array"><code class="computeroutput"><span class="identifier">array</span></code></a>, or <a class="link" href="../ref/boost__json__value.html" title="value"><code class="computeroutput"><span class="identifier">value</span></code></a> constructed from an initializer
        list will be propagated to each element, recursively.
      </p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2019, 2020 Vinnie Falco<br>Copyright © 2020 Krystian Stasiowski<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="conversion.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../dom.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../allocators.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
